﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TrainingsApp.Model;
using DevExpress.Web.ASPxMenu;

namespace TrainingsApp.IPADApp.UserControls.Public {
    public partial class UC_LeftMenu : ControlBase {
        protected void Page_Load(object sender, EventArgs e) {
             //return;
            BuildMenu(ASPxMenu1, this.CurPage.CurrentUser.FunctionRights.Where(u=>u.SORT!=-1).ToList(), false);
        }

        protected void BuildMenu(ASPxMenu menu, List<T_FUNCTION_Entity> list, bool enableRoles) {
            menu.Items.Clear();

            Dictionary<decimal, MenuItem> menuItems = new Dictionary<decimal, MenuItem>();

            //string css = "";
            for (int i = 0; i < list.Count; i++) {
                MenuItem item = CreateMenuItem(list[i]);

                if (menuItems.ContainsKey(list[i].PARENTID)) {
                    item.ItemStyle.CssClass = "dxnb";
                    menuItems[list[i].PARENTID].Items.Add(item);
                }
                else {
                    item.ItemStyle.CssClass = "dxnbItem";
                    menu.Items.Add(item);
                }
                menuItems.Add(list[i].ID, item);
            }
        }

        private MenuItem CreateMenuItem(T_FUNCTION_Entity entity) {
            MenuItem item = new MenuItem();
            item.Text = entity.FUNCTIONNAME;
            item.NavigateUrl = entity.LINKURL;
            item.ItemStyle.SelectedStyle.CssClass = "menu_on";
            item.ItemStyle.HoverStyle.CssClass = "menu_on";
            return item;
        }
    }
}
